﻿using System;
using System.Collections.Generic;

using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;
using System.Web.Profile;
using System.Text;

using Gift.DAL;
using Gift.Model;
using Gift.Model.ENUM;

namespace WebSite.admin.product
{
    public partial class goods : BaseAdmin
    {
       readonly  DALGoods theGoods = new DALGoods();
       readonly dalCompany theCompany = new dalCompany();
       readonly DALBrand theBrand = new DALBrand();
       readonly DALTerms theTerms = new DALTerms();


    protected void Page_Load(object sender, EventArgs e)
    {
       
        if (!base.IsPostBack)
        {
            base.ValidateLoginRule(ERule.ProductList);

            btnBrachDeleted.OnClientClick = Grid1.GetNoSelectionAlertInTopReference("没有选中项！");
            btnSale.OnClientClick = Grid1.GetNoSelectionAlertInTopReference("没有选中项！");
            btnNoSale.OnClientClick = Grid1.GetNoSelectionAlertInTopReference("没有选中项！");

            this.DelelGoodsList();
            this.BindCompanyList();
            this.Display("0", "|--");
            this.BindBrand();
            this.BindTerms();
            this.BindGrid();
        }
    }

    void BindCompanyList()
    {
        DataTable dt = theCompany.GetList("").Tables[0];
        this.ddlCompanyList.DataTextField = "name";
        this.ddlCompanyList.DataValueField = "id";
        this.ddlCompanyList.DataSource = dt;
        this.ddlCompanyList.DataBind();
        this.ddlCompanyList.Items.Insert(0, new ExtAspNet.ListItem("请选择供应商...", "-1"));
        this.ddlCompanyList.SelectedValue = this.ddlCompanyList.Items[0].Value;
    }



    protected void btnSearch_Click(object sender, EventArgs e)
    {
        this.BindGrid();
    }

    protected void btnSale_Click(object sender, EventArgs e)
    {
        int selectedCount = this.Grid1.SelectedRowIndexArray.Length;
        StringBuilder sbProIds = new StringBuilder();
        for (int i = 0; i < selectedCount; i++)
        {
            int rowIndex = this.Grid1.SelectedRowIndexArray[i];
            if (i == selectedCount - 1)
            {
                sbProIds.Append(this.Grid1.DataKeys[rowIndex][0].ToString());
            }
            else
            {
                sbProIds.Append(this.Grid1.DataKeys[rowIndex][0].ToString() + ",");
            }
        }
        if (sbProIds.Length > 0)
        {
            theGoods.BranchUpdateSale(sbProIds.ToString(),1);
        }
        ExtAspNet.Alert.Show("批量设置上架成功！");
        this.BindGrid();
    }

    protected void btnNoSale_Click(object sender, EventArgs e)
    {
        int selectedCount = this.Grid1.SelectedRowIndexArray.Length;
        StringBuilder sbProIds = new StringBuilder();
        for (int i = 0; i < selectedCount; i++)
        {
            int rowIndex = this.Grid1.SelectedRowIndexArray[i];
            if (i == selectedCount - 1)
            {
                sbProIds.Append(this.Grid1.DataKeys[rowIndex][0].ToString());
            }
            else
            {
                sbProIds.Append(this.Grid1.DataKeys[rowIndex][0].ToString() + ",");
            }
        }
        if (sbProIds.Length > 0)
        {
            theGoods.BranchUpdateSale(sbProIds.ToString(), 0);
        }
        ExtAspNet.Alert.Show("批量设置下架成功！");
        this.BindGrid();
    }

    protected void Grid1_PageIndexChange(object sender, ExtAspNet.GridPageEventArgs e)
    {
        Grid1.PageIndex = e.NewPageIndex;
        BindGrid();
    }

    string GetSqlWhere
    {
        get
        {
            StringBuilder strWhere = new StringBuilder();
            strWhere.AppendLine("1=1");
            if (this.ddlGoodsType.SelectedItem.Value != "-1")
            {
                strWhere.AppendFormat(" and typeId = {0}", this.ddlGoodsType.SelectedItem.Value);
            }
            if (this.ddlGoodsBrand.SelectedItem.Value != "-1")
            {
                strWhere.AppendFormat(" and brandId = {0} ", this.ddlGoodsBrand.SelectedItem.Value);
            }
            if (this.ddlTerms.SelectedItem.Value != "-1")
            {
                strWhere.AppendFormat(" and termsId = {0} ", this.ddlTerms.SelectedItem.Value);
            }
            if (this.ddlCompanyList.SelectedItem.Value != "-1")
            {
                strWhere.AppendFormat(" and T.companyId = {0} ", this.ddlCompanyList.SelectedItem.Value);
            }
            strWhere.AppendFormat(" and (goodsName like '%{0}%' or T.goodsCode like '%{0}%') ", this.txtKeywords.Text.Trim());
            return strWhere.ToString();
        }
    }


    protected void Grid1_Sort(object sender, ExtAspNet.GridSortEventArgs e)
    {
        DataTable table =  theGoods.GetListByPage(GetSqlWhere, string.Format("{0} {1}", e.SortField, e.SortDirection), Grid1.PageIndex, Grid1.PageSize).Tables[0];
        Grid1.DataSource = table;
        Grid1.DataBind();
        ViewState["sort"] = e.SortField;
        ViewState["dir"] = e.SortDirection;
    }
    private void BindGrid()
    {   // 1.设置总项数
        Grid1.RecordCount = theGoods.GetRecordCount(GetSqlWhere.Replace("T.",""));
        string strOrderBy = string.Format("{0} {1} "
                                           ,ViewState["sort"] == null ? null : ViewState["sort"].ToString()
                                           , ViewState["dir"] == null ? null : ViewState["dir"].ToString());
        if (strOrderBy.Length < 13)
        {
            strOrderBy = "goodsId desc ";
        }
        DataTable table = theGoods.GetListByPage(GetSqlWhere, strOrderBy, Grid1.PageIndex * Grid1.PageSize, Grid1.PageIndex * Grid1.PageSize + Grid1.PageSize-1).Tables[0];

        Grid1.DataSource = table;
        Grid1.DataBind();
        Grid1.SelectedRowIndexArray = new int[] { };

    }

    private void Display(string parentid, string space)
    {
        string sql = "Select * From tbGoodsType Where parentId =" + parentid + " Order By typeOrder";
        DataTable table = new DataTable();
        table = SQLAccess.GreatDs(sql).Tables[0];
        if (table.Rows.Count > 0)
        {
            foreach (DataRow row in table.Rows)
            {
                this.ddlGoodsType.Items.Add(new ExtAspNet.ListItem(space + "" + row["typeName"].ToString() + "", "" + row["typeId"].ToString() + ""));
                this.Display(row["typeId"].ToString(), "|-" + space);
            }
        }
        if (ddlGoodsType.Items.Count <=0 || ddlGoodsType.Items[0].Value != "-1")
        {
            this.ddlGoodsType.Items.Insert(0, new ExtAspNet.ListItem("请选择商品类别...", "-1"));
            this.ddlGoodsType.SelectedValue = this.ddlGoodsType.Items[0].Value;
        }
    }

    void BindBrand()
    {
        DataTable dt = theBrand.GetList("visible = 1").Tables[0];
        this.ddlGoodsBrand.DataTextField = "brandName";
        this.ddlGoodsBrand.DataValueField = "brandId";
        this.ddlGoodsBrand.DataSource = dt;
        this.ddlGoodsBrand.DataBind();
        this.ddlGoodsBrand.Items.Insert(0, new ExtAspNet.ListItem("请选择品牌...", "-1"));
        this.ddlGoodsBrand.SelectedValue = this.ddlGoodsBrand.Items[0].Value;
    }
    void BindTerms()
    {
        DataTable dt = theTerms.GetList("").Tables[0];
        this.ddlTerms.DataTextField = "termsName";
        this.ddlTerms.DataValueField = "id";
        this.ddlTerms.DataSource = dt;
        this.ddlTerms.DataBind();
        this.ddlTerms.Items.Insert(0, new ExtAspNet.ListItem("请选择气节类别...", "-1"));
        this.ddlTerms.SelectedValue = this.ddlTerms.Items[0].Value;
    }

    void DelelGoodsList()
    {
        if (Request.QueryString["action"] == "del")
        {
            theGoods.Delete(int.Parse(Request.QueryString["gid"]));
            ExtAspNet.Alert.Show("删除商品成功！");
        }
    }

    protected void btnBrachDeleted_Click1(object sender, EventArgs e)
    {
        int selectedCount = this.Grid1.SelectedRowIndexArray.Length;
        StringBuilder sbProIds = new StringBuilder();
        for (int i = 0; i < selectedCount; i++)
        {
            int rowIndex = this.Grid1.SelectedRowIndexArray[i];
            if (i == selectedCount - 1)
            {
                sbProIds.Append(this.Grid1.DataKeys[rowIndex][0].ToString());
            }
            else
            {
                sbProIds.Append(this.Grid1.DataKeys[rowIndex][0].ToString() + ",");
            }
        }
        if (sbProIds.Length > 0)
        {
            theGoods.DeleteList(sbProIds.ToString());
        }
        ExtAspNet.Alert.Show("批量删除商品成功！");
        this.BindGrid();
    }

    //protected void Grid1_RowDoubleClick(object sender, ExtAspNet.GridRowClickEventArgs e)
    //{
    //    this.openNewWindow.InnerHtml =    string.Format("openNewTab('AddNewProduct','product/goodsAdd.aspx?gid='{0},'商品编辑','');"
    //                       , Grid1.DataKeys[e.RowIndex][0].ToString());
    //    //this.Page.ClientScript.RegisterClientScriptBlock(this.Page.ClientScript.GetType(), "openwindow",

    //    //                      string.Format("<script>openNewTab('AddNewProduct','product/goodsAdd.aspx?gid='{0},'商品编辑','');</script>"
    //    //                   , Grid1.DataKeys[e.RowIndex][0].ToString()
    //    //                   )
    //    //   );

    //}

  
    }
}